﻿#region - 版 本 注 释 -
/*----------------------------------------------------------------
 * 版权所有 (c) 2024   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-GLJ2Q8J
 * 公司名称：
 * 命名空间：AutoCAD.EntityTool
 * 唯一标识：cd35dde5-61e6-44d3-b65c-7a302677ae89
 * 文件名：ShowMessageTools
 * 当前用户域：DESKTOP-GLJ2Q8J
 * 
 * 创建者：Guo-79991
 * 电子邮箱：799919859@qq.com
 * 创建时间：2024/7/15 12:04:57
 * 版本：V1.0.0
 *
 * 功能描述(Description):
 * 参考文档(Reference):
 * 数据表(Tables):
 * 作者(Author):Guo
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion - 版 本 注 释 -


namespace AutoCAD.EntityTools
{
    /// <summary>
    /// ShowMessageTools 的摘要说明
    /// </summary>
    public static class ShowMessageTools
    {

        /// <summary>
        /// 弹窗提示
        /// </summary>
        /// <param name="str">提示字符</param>
        public static void CadAlert(this string str)
        {
            Application.ShowAlertDialog(str);
        }
        /// <summary>
        /// 命令行提示
        /// </summary>
        /// <param name="str">提示字符</param>
        public static void CadMessage(this string str)
        {
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\r\n" + str);
        }
        /// <summary>
        /// 命令行提示
        /// </summary>
        /// <param name="str">提示字符</param>
        public static void CadMessage2(this string str)
        {
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(str + "\r\n");
        }


        /// <summary>
        /// 命令行提示
        /// </summary>
        /// <param name="isSuccess"></param>
        /// <param name="operation"></param>
        public static void CadMessage(this bool isSuccess, string operation)
        {
            if (isSuccess)
                CadMessage2($"\n {operation}-操作成功！");
            else
                CadMessage2($"\n {operation}-操作失败！");
        }
        public static void ShowProgressBar<TEntity>(this string showMessage, int time = 100, params TEntity[] entities)
    where TEntity : Entity
        {
            ProgressMeter pm = new ProgressMeter();
            pm.Start(showMessage);
            pm.SetLimit(entities.Length);
            for (int i = 0; i <= entities.Length; i++)
            {
                System.Threading.Thread.Sleep(time);//为了模拟效果，做的延时
                pm.MeterProgress();
            }
            pm.Stop();
        }

        ///// <summary>
        ///// 显示进度条
        ///// </summary>
        ///// <param name="showMessage">提示消息</param>
        ///// <param name="time">更新进度条后的延迟时间，以毫秒为单位</param>
        ///// <param name="polylines">当前的计数值</param>
        //public static void ShowProgressBar(this string showMessage, List<(Polyline, Polyline)> polylines, int time = 100)
        //{
        //    ProgressMeter pm = new ProgressMeter();
        //    pm.Start(showMessage);
        //    pm.SetLimit(polylines.Count);
        //    for (int i = 0; i <= polylines.Count; i++)
        //    {
        //        System.Threading.Thread.Sleep(time);//为了模拟效果，做的延时
        //        pm.MeterProgress();
        //    }
        //    pm.Stop();
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="showMessage">提示消息</param>
        /// <param name="time">更新进度条后的延迟时间，以毫秒为单位</param>
        /// <param name="entities">实体</param>
        public static void ShowProgressBar2<TEntity>(this string showMessage, int time = 100, params TEntity[] entities)
            where TEntity : Entity
        {
            ProgressMeter pm = new ProgressMeter();
            pm.Start(showMessage);
            pm.SetLimit(entities.Length);
            for (int i = 0; i <= entities.Length; i++)
            {
                System.Threading.Thread.Sleep(time);//为了模拟效果，做的延时
                pm.MeterProgress();
            }
            pm.Stop();
        }





        /// <summary>
        /// 判断是否有活动的编辑器对象
        /// </summary>
        /// <returns><see langword="true"/>有,<see langword="false"/>没有</returns>
        public static bool HasEditor()
        {
            return Application.DocumentManager.MdiActiveDocument != null &&
                   Application.DocumentManager.Count != 0 &&
                   Application.DocumentManager.MdiActiveDocument.Editor != null;
        }
        /// <summary>
        /// EntityTool对话框
        /// </summary>
        /// <param name="message">输出信息</param>
        public static void EntityToolMessage(this string message)
        {
            try
            {
                if (HasEditor())
                    CadMessage2(message);
                else
                    InfoMessageBox(message);
            }
            catch (System.Exception ex)
            {
                Message(ex);
            }
        }

        /// <summary>
        /// 提示信息对话框,默认标题为NFox.Cad
        /// </summary>
        /// <param name="message">对话框文本</param>
        public static void InfoMessageBox(string message)
        {
            InfoMessageBox("AuotoCAD.EntityTool", message);
        }

        /// <summary>
        /// 提示信息对话框
        /// </summary>
        /// <param name="caption">对话框的标题</param>
        /// <param name="message">对话框文本</param>
        public static void InfoMessageBox(string caption, string message)
        {
            try
            {
                System.Windows.Forms.MessageBox.Show(message, caption,
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                Message(ex);
            }
        }

        /// <summary>
        /// 异常信息对话框
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Message(System.Exception ex)
        {
            try
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Error);
            }
            catch
            {
                // ignored
            }
        }
    }
}
